<template>
  <div class="app-container">

    <h2 style="text-align: center;">创建课程大纲</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="最终发布"/>
    </el-steps>

    <!-- 章节 -->
    <div class="form">
      <el-button type="button" @click="openChapterDialog()">添加章节</el-button>
      <!-- 章节 -->
      <ul class="chanpterList">
        <li
          v-for="(chapter,index) in chapterVideoList"
          :key="index">
          <el-card class="box-card">
            <span style="color: #1e6abc;font-weight: bold;font-size: 18px">{{ chapter.title }}</span>
            <div class="acts">
              <el-button size="small" type="button" @click="openVideo(chapter.id)">添加小节</el-button>
              <el-button size="small" type="button" @click="openEditChapter(chapter.id)">编辑</el-button>
              <el-button size="small" type="button" @click="removeChapter(chapter.id)">删除</el-button>
            </div>
          </el-card>
          <!-- 视频 -->
          <ul class="chanpterList videoList">
            <li
              v-for="(video,index) in chapter.children"
              :key="video.id ">
              <el-card class="box-card">
                {{ video.title }}
                <span class="acts">

                    <el-button size="small" type="button" @click="openUpdateVideo(video.id)">编辑</el-button>
                    <el-button size="small" type="button" @click="deleteVideo(video.id)">删除</el-button>
                </span>
              </el-card>
            </li>
          </ul>
        </li>
      </ul>
      <el-form label-width="120px">
        <el-form-item style="margin-left: 636px">
          <el-button @click="previous">上一步</el-button>
          <el-button :disabled="saveBtnDisabled" type="primary" @click="next">下一步</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" :title="title">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加和修改课时表单   小节 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" :title="title2">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.isFree">
            <el-radio :label="true">免费</el-radio>
            <el-radio :label="false">默认</el-radio>
          </el-radio-group>
        </el-form-item>
<!--  :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"-->
        <el-form-item label="上传视频">
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleUploadExceed"
            :file-list="fileList"
            :action="BASE_API+'/eduservice/video/uploadAlyiVideo'"
            :limit="1"
            class="upload-demo">
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">最大支持1G，<br>
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br>
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br>
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br>
                SWF、TS、VOB、WMV、WEBM 等视频格式上传</div>
              <i class="el-icon-question"/>
            </el-tooltip>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


  export default {

    data() {
      return {
        fileList: [],//上传文件列表
        BASE_API: process.env.BASE_API, // 接口API地址
        saveBtnDisabled: false,
        saveVideoBtnDisabled: false,
        courseId: '',//课程id
        chapterVideoList: [],
        dialogChapterFormVisible:false,//章节弹框
        dialogVideoFormVisible:false, //小节弹框
        chapter: { //封装章节数据
          title: '',
          sort: 0
        },
        title: '添加章节',
        title2: '添加小节',
        video: {
          title: '',
          sort: 0,
          isFree: 0,
          videoSourceId: '',
          videoOriginalName:''//视频名称
        },
      }
    },

    methods: {

      //=================================================================================上传视频
      ////点击确定调用的方法 删除视频
      handleVodRemove(){
        this.deleteRequest("/eduservice/video/aliyun/"+this.video.videoSourceId).then(res=>{
          this.$message({
            type: 'success',
            message: res.message
          })
          //把文件列表清空
          this.fileList = []
          //把video视频id和视频名称值清空
          //上传视频id赋值
          this.video.videoSourceId = ''
          //上传视频名称赋值
          this.video.videoOriginalName = ''
        })
      },
      //点击×调用这个方法确认是否删除
      beforeVodRemove(file,fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      //上传成功
      handleVodUploadSuccess(response,file,fileList){
        //上传视频id赋值
        this.video.videoSourceId = response
        //上传视频名称赋值
        this.video.videoOriginalName = file.name
      },
      handleUploadExceed() {
        this.$message.warning('想要重新上传视频，请先删除已上传的视频')
      },

      //=================================================================================小节部分

      saveOrUpdateVideo(){
        if(this.video.id){
          this.updateVideo()
         }else{
          this.addVideo()
        }

      },
      openVideo(chapterId){
        this.video = {}
        this.chapter.chapterId = ''
        this.video.courseId=''
        this.dialogVideoFormVisible=true
        this.video.chapterId=chapterId

      },
      addVideo(){
        if(this.video.title==null||
          this.video.sort==null||
          this.video.isFree==null
        ){
          this.$message({
            type: 'warning',
            message: "请填写完成信息"
          })
        }else if(this.video.videoSourceId==null){
          this.$message({
            type: 'warning',
            message: "视频上传中请稍等"
          })
        }else{
        this.video.courseId=this.courseId
        this.postRequest("/eduservice/video/",this.video).then(res=>{
          this.$message({
            type: 'success',
            message: res.message
          })
          this.initChapter()
          this.dialogVideoFormVisible = false
          this.video = {}
          this.chapter.chapterId = ''
          this.video.courseId=''
          //把文件列表清空
          this.fileList = []
        })
      }
      },
      updateVideo(){
        if(this.video.title==null||
          this.video.sort==null||
          this.video.isFree==null
        ){
          this.$message({
            type: 'warning',
            message: "请填写完成信息"
          })
        }else if(this.video.videoSourceId==null){
          this.$message({
            type: 'warning',
            message: "视频上传中请稍等"
          })
        }else{
          this.putRequest("/eduservice/video/",this.video).then(res=>{
            this.$message({
              type: 'success',
              message: res.message
            })
            this.initChapter()
            this.dialogVideoFormVisible = false
            this.video = {}
            this.chapter.chapterId = ''
            this.video.courseId=''
          })
        }

      },
      openUpdateVideo(id){
        this.title2="修改小节"
        this.dialogVideoFormVisible=true
        this.getRequest("/eduservice/video/"+id).then(res=>{
         this.video=res.data.data
        })
      },
      deleteVideo(id){
          this.$confirm('此操作将此小节删除, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {  //点击确定，执行then方法
            this.deleteRequest('/eduservice/video/'+id).then(res => {
              if(res){
                this.$message({
                  type: 'success',
                  message: res.message
                });
                this.initChapter()
              }
            })
          })
      },
      //=================================================================================章节部分
      removeChapter(id) {
        this.$confirm('此操作将此章节删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {  //点击确定，执行then方法
          this.getRequest('/eduservice/chapter/checkChapter/' + id).then(res => {
              if (res.data.data>0) {
                //提示信息
                this.$message({
                  type: 'warning',
                  message: "该章节中有小节"
                })
              }else {
                this.deleteRequest('/eduservice/chapter/'+id).then(res => {
                  if(res){
                    this.$message({
                      type: 'success',
                      message: res.message
                    })
                    this.initChapter()
                  }
                })
            }
          })
        })
      },
      openEditChapter(id) {
        this.title = '修改章节'
        this.dialogChapterFormVisible = true
        this.getRequest('/eduservice/chapter/findChapterInfo/' + id).then(res => {
          if (res) {
            this.chapter = res.data.data
          }
        })
      },
      updateChapter(){
        this.putRequest('/eduservice/chapter/',this.chapter).then(res => {
          if (res) {
            //提示信息
            this.$message({
              type: 'success',
              message: res.message
            })
            this.initChapter()
            this.dialogChapterFormVisible = false
            this.chapter = {}
            this.chapter.courseId = ''
          }
        })
      },
      addChapter() {
        this.chapter.courseId = this.courseId
        this.postRequest('/eduservice/chapter/', this.chapter).then(res => {
          if (res) {
            //提示信息
            this.$message({
              type: 'success',
              message: res.message
            })
            this.initChapter()
            this.dialogChapterFormVisible = false
            this.chapter = {}
            this.chapter.courseId = ''

          }
        })
      },

      saveOrUpdate() {
        if(this.chapter.id){
          this.updateChapter()
        }else{
          this.addChapter()
        }

      },
      openChapterDialog() {
        this.title = '添加章节'
        this.dialogChapterFormVisible = true
        this.chapter = {}
        this.chapter.courseId = ''
      },
      initChapter() {
        this.getRequest('/eduservice/chapter/' + this.courseId).then(res => {
          if (res) {
            this.chapterVideoList = res.data.data
          }
        })
      },
      previous() {
        this.$router.push({ path: '/course/info/' + this.courseId })
      },
      next() {
        //跳转到第二步
        this.$router.push({ path: '/course/publish/' + this.courseId })
      }

    },
    mounted() {
      //获取路由的id值
      if (this.$route.params && this.$route.params.id) {
        this.courseId = this.$route.params.id
        //根据课程id查询章节和小节
        this.initChapter()
      }

    }
  }
</script>

<style scoped>
  .box-card{
    margin: 10px 10px 10px 0px;
    height: 80px;
    line-height: 40px;
  }
  .chanpterList {
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .chanpterList li {
    position: relative;
  }

  .chapter {
    float: left;
    font-size: 20px;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    border: 1px solid #DDD;
  }

  .acts {
    float: right;
  /*  font-size: 14px;*/
  }

  .videoList {
    padding-left: 50px;
  }

  .videoList .video {
    float: left;
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    border: 1px dotted #DDD;
  }

  .form {
    width: 1000px;
    padding: 30px 30px 30px 30px;
    padding-right: 20px;
    margin: 10px auto;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 1px #cac6c6;
  }
</style>
